package vn.gq.cd.model;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.gq.app4vn.doctruyen.Configuration;
import vn.gq.app4vn.doctruyen.R;
import vn.gq.tym.utils.Client;
import vn.gq.tym.utils.DataType;
import vn.gq.tym.utils.IClientListener;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GooglePlayAdapter extends BaseAdapter {
	
	private ArrayList<GooglePlayAccount> accounts = new ArrayList<GooglePlayAdapter.GooglePlayAccount>();
	private Handler handler = new Handler();
	private LayoutInflater mInflater;
	private Context context;
	
	public GooglePlayAdapter(Context context) {
		init();
		mInflater = LayoutInflater.from(context);
		this.context = context;
	}
	
	public void load(Runnable run) {
		Client client = new Client(context);
		client.setOutputDataType(DataType.JSON);
		client.executeAndListener(Configuration.STORE_URL, new IClientListener() {
			
			@Override
			public void onDone(int code, Object result) {
				
				if (result instanceof JSONArray) {
					JSONArray arr = (JSONArray) result;
					if (arr.length() > 0) {
						ArrayList<GooglePlayAccount> naccs = new ArrayList<GooglePlayAdapter.GooglePlayAccount>();
						for (int i = 0;i < arr.length();i++) {
							try {
								JSONObject ob = arr.getJSONObject(i);
								GooglePlayAccount a = new GooglePlayAccount(ob.getString("uri"), ob.getString("name"));
								
								
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		});
	}
	
	private void init() {
		accounts.add(new GooglePlayAccount("pub:\"UNG DUNG VIET\"", "UNGDUNGVIET"));
		accounts.add(new GooglePlayAccount("pub:VEGASOFT", "VEGASOFT"));
		accounts.add(new GooglePlayAccount("pub:COMICVIET", "COMICVIET"));
		accounts.add(new GooglePlayAccount("pub:S-Mobile Team", "S-MOBILE TEAM"));
		accounts.add(new GooglePlayAccount("pub:iMOBILE", "iMOBILE"));
	}
	
	@Override
	public int getCount() {
		return accounts.size();
	}

	@Override
	public Object getItem(int position) {
		return accounts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item_simple, parent, false);
			holder = new ViewHolder();
			holder.textView = (TextView) convertView.findViewById(R.id.textView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		GooglePlayAccount item = (GooglePlayAccount) getItem(position);
		holder.textView.setText(item.name);
		return convertView;
	}
	
	private static class ViewHolder {
		public TextView textView;
	}
	
	public static class GooglePlayAccount {
		public String uri, name;
		
		public GooglePlayAccount(String uri, String name) {
			this.uri = uri;
			this.name = name;
		}
	}
	
}
